//
//  MKMapViewController.m
//  UILbrary
//
//  Created by mac on 17/3/3.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "MKMapViewController.h"
#import "MKoverlayModel.h"
@import MapKit;
@import CoreLocation;
@interface MKMapViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>
@property (nonatomic, strong) MKMapView *mapViwe;
@property (nonatomic, strong) MKoverlayModel *overlayModel;
@property (nonatomic, strong) CLLocationManager *locationManager;//定位
@end

@implementation MKMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor =[ UIColor whiteColor];
    [self.view addSubview:self.mapViwe];
    [self.locationManager startUpdatingLocation];
    [self configMapView];
    [self drawAcircleLine];
}

#pragma mark - private methods
- (void)configMapView{
    self.mapViwe.showsUserLocation = YES;
//    [self.mapViwe addOverlay:self.overlayModel];
}

- (void)drawAcircleLine{

    MKCircle *circle = [MKCircle circleWithCenterCoordinate:self.overlayModel.coordinate radius:50];
    [self.mapViwe addOverlay:circle];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    
    [self.mapViwe setCenterCoordinate:[locations[0] coordinate] animated:YES];
    
}
#pragma mark -  MKMapViewDelegate


- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
    
    MKCircleRenderer *render = [[MKCircleRenderer alloc] initWithOverlay:overlay];
    
    render.lineWidth = 3;
    render.strokeColor = [UIColor redColor];
    
    return render;
}


#pragma mark - lazy methods
- (MKMapView *)mapViwe{
    if (!_mapViwe) {
        _mapViwe = [[MKMapView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 350)];
        _mapViwe.delegate = self;
    }
    return _mapViwe;
}

- (MKoverlayModel *)overlayModel{
    if (!_overlayModel) {
        _overlayModel = [MKoverlayModel new];
    }
    return _overlayModel;
}

- (CLLocationManager *)locationManager{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        [_locationManager requestAlwaysAuthorization];
        _locationManager.delegate = self;
    }
    return _locationManager;
}

@end
